package com.geofeature;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.geofeature.models.Action;
import com.geofeature.models.RingOffAction;
import com.geofeature.models.RingOnAction;
import com.geofeature.models.Task;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 26.02.11
 * Time: 15:45
 * To change this template use File | Settings | File Templates.
 */
public class TaskActivity extends Activity {
    Button btnCancel, btnSave;
    EditText edtTag;
    Task task;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.new_task);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            task = (Task) extras.getSerializable("task");
        }

        btnCancel = (Button) findViewById(R.id.act_new_task_cancel);
        btnSave = (Button) findViewById(R.id.act_new_task_save);
        edtTag = (EditText) findViewById(R.id.act_new_task_label);

        if (task != null) {
            edtTag.setText(task.getName());
        }

        edtTag.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                //if (edtTag.getText().equals(TaskActivity.this.getString(R.string.act_new_task_def_name))){
                //    edtTag.setText("");
                //    edtTag.setTextColor(0xFFFFFF);
                //}
            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Task newTask = new Task();
                Action ringOffAction = new RingOffAction();
                Action ringOnAction = new RingOnAction();
                
                newTask.name = edtTag.getText().toString();
                newTask.setInAction(ringOffAction);
                newTask.setOutAction(ringOnAction);

                GeoDbAdapter db = new GeoDbAdapter(TaskActivity.this);
                db.open();
                db.createTask(newTask);
                db.close();

                Intent intent = new Intent(TaskActivity.this, GeoFeature.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });


        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(TaskActivity.this, GeoFeature.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

    }

}
